package com.example.project22_editingmediaplayer.adapter;

import java.util.ArrayList;
import java.util.Random;

import com.example.project22_editingmediaplayer.R;
import com.example.project22_editingmediaplayer.objects.AlBumObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;



public class RowAlbum extends BaseAdapter {

	public ArrayList<AlBumObject> listData;
	private LayoutInflater mInflater;
	private Context mContext;

	public RowAlbum(ArrayList<AlBumObject> listData, Context mContext) {
		this.listData = listData;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = mContext;
	}

	public void resetData(ArrayList<AlBumObject> listData) {
		this.listData = listData;
		this.notifyDataSetChanged();
	}

	public ArrayList<AlBumObject> getAllData() {
		return this.listData;
	}

	public void addData(AlBumObject albumObject) {
		listData.add(albumObject);
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	@Override
	public AlBumObject getItem(int postition) {
		// TODO Auto-generated method stub
		return listData.get(postition);
	}

	@Override
	public long getItemId(int postition) {
		// TODO Auto-generated method stub
		return postition;
	}

	@Override
	public View getView(int postition, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		RowHolder holder = null;
		View view = convertView;
		if (view == null) {
			view = mInflater.inflate(R.layout.row_item, null);
			holder = new RowHolder();
			holder.imvAvarSong = (ImageView) view
					.findViewById(R.id.imvAvarSong);
			holder.txvSong = (TextView) view.findViewById(R.id.txtNameSong);
			holder.txvArtis = (TextView) view.findViewById(R.id.txtArtis);
			view.setTag(holder);
		} else {
			holder = (RowHolder) view.getTag();
		}
		final AlBumObject albumObject = listData.get(postition);
		holder.txvSong.setText(albumObject.get_nameAlbum()
				+ " ("
				+ albumObject.get_numberSong()
				+ " "
				+ mContext.getApplicationContext().getResources()
						.getString(R.string.number_song) + ")");
		holder.txvArtis.setText(albumObject.get_artistAlbum());
		if (albumObject.get_bitmap() != null) {
			holder.imvAvarSong.setImageBitmap(albumObject.get_bitmap());
			holder.imvAvarSong.setScaleType(ScaleType.CENTER_INSIDE);
		}else
		{
			Random rand = new Random();
			int currentSongIndex = rand.nextInt(SongResource.musicBackGround.length);
			holder.imvAvarSong.setBackgroundResource(SongResource.musicBackGround[currentSongIndex]);
		}
		// set image view
		return view;
	}

	public static final class RowHolder {
		public ImageView imvAvarSong;
		public TextView txvSong;
		public TextView txvArtis;
	}
}